#!/usr/bin/perl
#
# Do weird stuff with an sql database
# In this case: Take incoming email and store it into a database
# of machine registrations.
#
use DBI;		

# Logging nonsense
if (! -t STDERR) {
    open(STDERR, ">>/home/counter/experiment/machsql/mailer-log");
}

$database = "counter";

$driver = "mysql";
$user = "counter";
$password = "password";

%months = (
"Jan", "01",
"Feb", "02",
"Mar", "03",
"Apr", "04",
"May", "05",
"Jun", "06",
"Jul", "07",
"Aug", "08",
"Sep", "09",
"Oct", "10",
"Nov", "11",
"Dec", "12",
	   );

$dsn = "DBI:$driver:database=$database";
$dbh = DBI->connect($dsn, $user, $password);

# Prepare some interesting queries

$askfor = $dbh->prepare("SELECT uniqueid FROM automach WHERE uniqueid = ?");
$store = $dbh->prepare("INSERT INTO automach
          (name, uniqueid, updated, lastmessage, rectext,
           uptime, cpu, bogomips, mailfrom, method, kernel)
    VALUES(?, ?,        ?,    ?,       ?,       ?,      ?,   ?, ?, ?, ?)");
$update = $dbh->prepare("UPDATE automach
       SET name=?, updated=?, lastmessage=?, rectext=?,
            uptime=?, cpu=?, bogomips=?, mailfrom=?, method=?, kernel=?
       WHERE uniqueid = ?");

# Go through STDIN

while (<>) {
    if (/^From /) {
	processmessage();
	warn "Starting msg from $'\n";
	$recmode = 0;
	$header = 1;
    }
    $message .= $_;
    if ($header) {
	#warn "Header line: $_";
	if (/^From:\s+/) {
	    $mailfrom = $';
	    chop $mailfrom;
	    warn "Mail comes from $mailfrom\n";
	} elsif (/^Date:\s*/) {
	    $msgdate = $';
	    if ($msgdate =~ /(\d+) (\S{3}) (\d{4}) (\d+:\d+:\d+)/) {
		$msgdate = sprintf("%04d%02d%02d%s",
				   $3, $months{$2}, $1, $4);
		$msgdate =~ s/://g;
		warn "Msgdate is $msgdate\n";
	    } else {
		warn "Msgdate is $msgdate - ignored\n";
		undef $msgdate;
	    }

	} elsif (/^\s*$/) {
	    $header = 0;
	}
    }
    if ($recmode) {
	chop;
	if (/^\/\/END/) {
	    $recmode =0;
	    next;
	}
	if (/^(\S+):\s*/) {
	    $entry{$1} = $';
	    $entrytext .= "$1:$'\n";
	} else {
	    warn "Problem line: $_\n";
	}
    } elsif (/^\/\/MACHINE/) {
	$recmode = 1;
    }
}
processmessage();

print "$messagecount messages processed\n";

sub processmessage {
    if (%entry) {
#	print "Storing record\n";
    } else {
	return;
    }
    if (!$entry{uniqueid}) {
	warn "No uniqueid - can't store\n",
	return;
    }
    # See if it exists...
    $exists = $askfor->execute($entry{uniqueid}) || die "Select check failed\n";
#    print "Exists got $exists\n";
    if ($exists == 0) {
	warn "New record for $entry{name}\n";
	warn "Bogomips is $entry{bogomips}\n";
	$store->execute($entry{name}, $entry{uniqueid}, $msgdate,
                        $message, $entrytext,
			$entry{uptime}, $entry{cpu}, $entry{bogomips}+0,
			$mailfrom, $entry{method}, $entry{kernel})
	    || die "Insert statement failed\n";
    } else {
	warn "Update record for $entry{name}\n";
	$update->execute($entry{name}, $msgdate, $message, $entrytext,
			$entry{uptime}, $entry{cpu}, $entry{bogomips} + 0,
			  $mailfrom, $entry{method}, $entry{kernel},
			  $entry{uniqueid})
	    || die "Update statement failed\n";
    }
    ++ $messagecount;
    undef %entry;
    undef $entrytext;
    undef $message;
    undef $msgdate;
}


	
	


